<template>
	<view class="u-page">
		<view class="header"></view>
		<view class="content">
			<view class="label-title">
				<view class="title">充值项目推广金</view>
			</view>
			<view class="balance">
				
				<!-- <view class="price">
					<view></view>
				</view> -->
				<view class="lable-info">自定义金额</view>
				<view class="withdrawal"><view class="symbol">￥</view><view class="input"><input type="number" @input="onKeyInput"  placeholder="输入充值金额" /></view></view>
				
			</view>
			<view class="submit">
				<button type="warn" v-if="butstatus == true && wallet==false" @click="submit()">确认充值</button>
				<button type="default" v-else>确认充值</button>
				<view class="subrule" v-if="rule !=''">{{rule}}</view>
			</view>
			
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pid:'',
				lists: [],
				userData:"",
				datas:'',
				inputValue:'',
				butstatus:false,
				wallet:false,
				rule:"每次充值金额必须是100或100的倍数",
			}
		},
		onLoad() {
			let user = uni.getStorageSync('userData')
			this.userData = user
			this.userinfo();
			
		},
		methods: {
			onKeyInput: function(event) {
				let inputValue = event.target.value
				// var re = /^\d+00$/;
				// if(re.test(inputValue)){
					this.inputValue = inputValue
					this.butstatus = true
				// }else{
				// 	console.log('2')
				// 	this.butstatus = false
				// }
			},
		
			async userinfo(){
				const users = await this.GQL({
					"operationName": "allSlUsers",
					"query": "query allSlUsers($filter: SlUserFilter) { allSlUsers(filter:$filter) { nodes { uid projectPid } } }",
					"variables": {"filter": {"uid":{"equalTo":uni.getStorageSync('userData').uid}}}
				})
				if(users.data.allSlUsers.nodes !=''){
					this.datas = users.data.allSlUsers.nodes[0]
				}
				
				
				console.log('user',users)
			},
			async submit(){
				let a = {
						"name":"商家充值",
						"total":this.inputValue,
						"time":new Date().getTime()
					}
				
				let data = {
					uid:this.userData.uid,
					data:JSON.stringify(a),
					type:4,
					pid:this.datas.projectPid,
					totalPrice:this.inputValue,
				};
				// console.log('1-----',data)
				//下单并调起支付
				const order = await this.GQL({
					"operationName": "createSlUserOrder",
					"query": "mutation createSlUserOrder($input: CreateSlUserOrderInput!) { createSlUserOrder(input: $input) { slUserOrder { uid orderId oid } } }",
					"variables": { "input": { "slUserOrder":data } }
				})
				console.log('order',order)
				if(order.data.createSlUserOrder.slUserOrder.oid !=''){
					this.orderid = order.data.createSlUserOrder.slUserOrder.oid
					
					this.goWxpay();
				}else{
					uni.showToast({ title: '生成订单失败', icon: 'error', duration: 3000 });
				}
			},
			// 1994年10月20日
			async goWxpay(){
				let that = this
				uni.request({
					url:that.HttpServe+"/WeChat/storeRecharge",
					method:"POST",
					data:{
						oid:that.orderid
					},
					success(res) {
						console.log('res',res)
						if(res.data.code == 200){
							// console.log('10----',res.data.data)
							let parameters = JSON.parse(res.data.data.parameters)
							uni.requestPayment({
							    provider: 'wxpay',
								timeStamp: parameters.timeStamp,
								nonceStr: parameters.nonceStr,
								package: parameters.package,
								signType: 'MD5',
								paySign: parameters.paySign,
								success: function (res1) {
									uni.reLaunch({
										url:"/pages/store/storeindex"
									})
								},
								fail: function (err) {
									uni.showToast({ title: '充值失败！', icon: 'error', duration: 3000 });
									console.log('fail:' + JSON.stringify(err));
								}
							});
						}else{
							uni.showToast({ title: '调起支付失败', icon: 'error', duration: 3000 });
							// uni.navigateBack({
							// 	delta: 1
							// })
						}
						
					}
				})
			},
			
			
		}
	}
</script>

<style lang="scss">
.u-page{
		background-color: #FBFCFE;
	}
	 .header{
	 	width: 100%;
	 	height: 200px;
	 	/* color: #FFFFFF; */
	 	background: linear-gradient(#C7171E, #f0f2f5); /* 标准的语法 */
	 	
	 }
	.content{
		position: relative;
		top: -200px;
		background-color: #FFFFFF;
		border-radius: 5px;
		margin: 15px;
		.label-title{
			height: 50px;
			background: #F9F9F9;
			border-radius: 5px 5px 0px 0px;
			border: 1px solid #EAEAEA;
			.title{
				font-size: 15px;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #161E3E;
				line-height: 16px;
				margin: 20px 0px 14px 17px;
			}
		
		}
		.balanceLab{
			font-size: 18px;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #767B8D;
			line-height: 16px;
			margin: 30px 0px 0px 20px;
		}
		.price{
			display: flex;
			margin: 20px 0px 0px 20px;
			.num{
				padding: 10px 20px 0px 0px;
				font-size: 24px;
				font-family: PingFangSC-Semibold, PingFang SC;
				font-weight: 600;
				color: #767B8D;
				line-height: 16px;
			}
		}
		
		
		.lable-info{
			margin: 50px 0px 0px 20px;
			font-size: 15px;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #161E3E;
			line-height: 16px;
		}
		.withdrawal{
			    display: flex;
			    margin: 20px 20px 0px 20px;
			    font-size: 15px;
			    font-family: PingFangSC-Regular, PingFang SC;
			    font-weight: 400;
			    color: #9BA7C1;
			    line-height: 16px;
			    border-bottom: 1px solid #D7D7D7;
				.symbol{
					padding: 4px 0px;
				}
				.input{
					font-size: 15px;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #9BA7C1;
					line-height: 16px;
					padding: 0px 0px 10px 5px;
				}
		}
		.submit{
			margin: 20px 17px;
		}
		.subrule{
			padding: 10px 0px;
			text-align: left;
			font-size: 11px;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #9BA7C1;
			line-height: 18px;
		}
		.rule-w{
			margin: 30px 0px 0px 20px;
			.rulelab{
				font-size: 15px;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #161E3E;
				line-height: 16px;
			}
			.ruleContent{
				padding: 10px 0px;
				text-align: left;
				font-size: 11px;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #9BA7C1;
				line-height: 18px;
			}
		}
	}
	
</style>